package handles

import (
	"github.com/gin-gonic/gin"
	"order-pro/models"
	"strconv"
)

func Add(c *gin.Context) {
	article_title := c.PostForm("article_title")
	article_content := c.PostForm("article_content")
	article_author := c.PostForm("article_author")
	if article_title == "" || article_content == "" || article_author == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "参数不正确",
		})
		return
	}
	data := models.Article{
		Title:   article_title,
		Content: article_content,
		Author:  article_author,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("page_size"))
	offset := (page - 1) * size

	var article []models.Article
	models.DB.Offset(offset).Limit(size).Find(&article)
	var total int64
	models.DB.Model(&models.Article{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  article,
		"total": total,
	})
	return
}
